package project.android;

import java.util.Date;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


public class ManageTasks extends ListActivity {
		
	static String[] LIST=null;
    static String[] taskGuid = null;
    static boolean[] overdatedFlag = null;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        
        //set footer as "add new task" button
        Button addTaskButton = new Button(this);
        addTaskButton.setText("Add New Task");
        addTaskButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(ManageTasks.this, NewTaskPage.class);
        		  startActivity(i);
            }
        });
        
        //set the button to the listview
        final ListView lv = getListView();
        lv.addFooterView(addTaskButton);
        lv.setTextFilterEnabled(true);
        
        //connect to database to fetch all tasks
        DatabaseConnection db = new DatabaseConnection(this);
        db.addTableIfNotExists("TaskBoard","Tasks","ID INTEGER PRIMARY KEY,NAME VARCHAR," +
				"DESCRIPTION VARCHAR,DEADLINE TIMESTAMP," +
				"ASSIGNEDBY VARCHAR,GROUPNAME VARCHAR,GROUPID VARCHAR,FINISHED INTEGER,"+
				"SYNC INTEGER,GUID VARCHAR,DELETED INTEGER");
        Cursor c = db.selectAll();
        DatabaseConnection db2 = new DatabaseConnection(this);
        db2.connectDatabase("TaskBoard","Tasks");
        Cursor d = db2.select("DELETED", "1");  /*also count the ones that are deleted*/
        db.close();
		db2.close();
        
        //create LIST array with task names
        if(c.getCount() == 0){
        		
        	LIST = new String[1];
        	LIST[0] = "You don't have any task!";
        	//set list view
            setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item, LIST));
 
        }
        else{
			
        	
        	LIST = new String[c.getCount()-d.getCount()];
        	taskGuid = new String[c.getCount()-d.getCount()];
        	overdatedFlag = new boolean[c.getCount()-d.getCount()];
        	c.moveToFirst();
			int i =0;
			do {

				if(c.getInt(c.getColumnIndex("DELETED"))!= 1 ){
					LIST[i] = new String(c.getString(c.getColumnIndex("NAME")));
					taskGuid[i] = new String(c.getString(c.getColumnIndex("GUID")));
					String deadline = c.getString(c.getColumnIndex("DEADLINE"));
					int finished = c.getInt(c.getColumnIndex("FINISHED"));
					Date currentDate = new Date();
			        if(Date.parse(deadline) <= currentDate.getTime() && finished == 0)
			        	overdatedFlag[i] = true;
			        else
			        	overdatedFlag[i] = false;
					i++;
				}
				    
			}while(c.moveToNext());
				
			//create onclick event for each list item
			lv.setOnItemClickListener(new OnItemClickListener() { 
	        	public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
	        		Intent i = new Intent(ManageTasks.this, TaskPage.class);
	        		int j;
	        		for(j = 0; j < LIST.length;j++)
	            		if(LIST[j] == lv.getItemAtPosition(position).toString()) break;
	        		i.putExtra("taskGuid", taskGuid[j]);
	        		startActivity(i);
	        	} 
	        }); 
			
			//set list view
			TaskListViewAdapter adapter = new TaskListViewAdapter(this, LIST,overdatedFlag);
	        setListAdapter(adapter); 
		}
        
	}


}